<template>
  <div id="login">
    <div class="login-mask"></div>
    <div class="form-wrap">
      <p>
        <span>Login</span>
        <router-link :to="{name:'UserRegister'}">注册</router-link>
      </p>
      <el-form ref="forms" :model="forms" :rules="rules">
        <el-form-item prop="userName">
          <el-input v-model.trim="forms.userName" size="large" placeholder="请输入账号/邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model.trim="forms.password" type="password" size="large" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="form-margin">
          <el-button @click="submit" :loading="loading" style="width:100%" size="large" type="primary">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import {DBUser} from '@/db'
export default {
  name: 'UserLogin',
  data() {
    return {  
        loading: false,
        forms: {
            userName: "",
            password: "",
        },
        rules: {
            userName: {
                required: true,
                message: "The userName cannot be empty",
                trigger: "blur"
            },
            password: {
                required: true,
                message: "The password cannot be empty",
                trigger: "blur"
            }
        }
    }
  },
  methods: {
     async submit(){
         let result = await this.$refs.forms.validate();
          if (!result) return;
          const user = DBUser.findUser(this.forms)
          if(user){
            this.$message({
                message: '登录成功',
                type: 'success'
            });
            this.$store.dispatch('user/Login', user)
            this.$router.push({name:"Todo"})
          }else{
            this.$message({
                message: '账号或密码不正确！',
                type: 'warning'
            });
          }
      }
  }
}
</script>
<style lang="less" scoped>
#login {
  height: 100%;
  background-size: cover;
  .login-mask {
    height: 100%;
    background-color: rgba(0, 0, 0, 0.35);
  }
  .form-sub {
    .icon {
      vertical-align: middle;
      margin-left: 16px;
      font-size: 30px;
      color: rgba(0, 0, 0, 0.2);
      transition: all 0.5s;
      cursor: pointer;
    }
    i.icon-weibo-copy {
      .icon;
      &:hover {
        color: rgba(230, 22, 45, 0.7);
      }
    }
  }
  .form-wrap {
    h1 {
      text-align: center;
      padding-bottom: 15px;
      font-size: 33px;
    }
    p {
      text-align: center;
      padding: 15px 0;
      span {
        padding: 0 10px;
        padding-bottom: 10px;
        font-size: 18px;
        display: inline-block;
      }
    }
    width: 380px;
    background-color: #fff;
    padding: 15px 30px;
    border-radius: 4px;
    position: absolute;
    left: 50%;
    top: 40%;
    transform: translate(-50%, -50%);
    box-shadow: 1px 1px 8px rgba(0, 0, 0, 0.4);
  }
  .form-margin {
    margin-bottom: 15px;
  }
  .form-code {
    padding-right: 105px;
    position: relative;
  }
  .checkcode {
    position: absolute;
    right: 0;
    top: 0;
    height: 36px;
    border: 1px solid #dddee1;
  }
  @media screen and(max-width:678px) {
    .form-wrap {
      width: 90%;
      margin-left: 0;
    }
  }
}
</style>


